﻿using JESAI.Application.Abstractions.DynamicApi;
using JESAI.Application.Abstractions.Uniform;
using JESAI.Caching;
using JESAI.Framework.Attributes;
using JESAI.Framework.Mvc;
using Microsoft.AspNetCore.Mvc;

namespace JESAI.Platform.WebApi.Controllers.Uniform
{
    [ApiController]
    [Route("api/v1/[controller]/[action]/{method}")]
    public class UniformAppController : ApiControllerBase
    {

        private readonly IUniformApiService _uniformApiService;
        private readonly IDynamicApiService _dynamicApiService;
        private CacheManager _cacheManager;

        public UniformAppController(IUniformApiService uniformApiService, CacheManager cacheManager, IDynamicApiService dynamicApiService)
        {
            _uniformApiService = uniformApiService;
            _cacheManager = cacheManager;
            _dynamicApiService = dynamicApiService;
        }

        [HttpPost]
        [WebApiAuthorization]
        public async Task<IActionResult> Post(string method, string param)
        {
            try
            {
                var codeidentify = await DoDynamicApiRequest(method, JESAI.Biz.Data.Enums.HttpType.Post);
                var result = await _uniformApiService.Execute(codeidentify, param);
                return Success(data: result);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }

        private async Task<string> DoDynamicApiRequest(string method, JESAI.Biz.Data.Enums.HttpType httpType)
        {
            string codeidentify = await _cacheManager.GetOrAdd(method, async (val) =>
            {
                var dynamicApi = await _dynamicApiService.GetDynamicApiByApiSign(method, httpType);
                if (dynamicApi == null)
                {
                    throw new Exception("没找到对应的API");
                }
                return dynamicApi.CodeTreeId;
            });

            if (codeidentify.IsNullOrEmpty())
            {
                throw new Exception("没找到对应的API");
            }


            return codeidentify;
        }

        [HttpPut]
        [WebApiAuthorization]
        public async Task<IActionResult> Put(string method, string param)
        {
            try
            {
                var codeidentify = await DoDynamicApiRequest(method, JESAI.Biz.Data.Enums.HttpType.Put);
                var result = await _uniformApiService.Execute(codeidentify, param);
                return Success(data: result);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }

        [HttpGet]
        [WebApiAuthorization]
        public async Task<IActionResult> Get(string method, string param)
        {
            try
            {
                var codeidentify = await DoDynamicApiRequest(method, JESAI.Biz.Data.Enums.HttpType.Get);
                var result = await _uniformApiService.Execute(codeidentify, param);
                return Success(data: result);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }

        [HttpDelete]
        [WebApiAuthorization]
        public async Task<IActionResult> Delete(string method, string param)
        {
            try
            {
                var codeidentify = await DoDynamicApiRequest(method, JESAI.Biz.Data.Enums.HttpType.Delete);
                var result = await _uniformApiService.Execute(codeidentify, param);
                return Success(data: result);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }

        [HttpPatch]
        [WebApiAuthorization]
        public async Task<IActionResult> Patch(string method, string param)
        {
            try
            {
                var codeidentify = await DoDynamicApiRequest(method, JESAI.Biz.Data.Enums.HttpType.Patch);
                var result = await _uniformApiService.Execute(codeidentify, param);
                return Success(data: result);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }

        [HttpOptions]
        [WebApiAuthorization]
        public async Task<IActionResult> Options(string method, string param)
        {
            try
            {
                var codeidentify = await DoDynamicApiRequest(method, JESAI.Biz.Data.Enums.HttpType.Options);
                var result = await _uniformApiService.Execute(codeidentify, param);
                return Success(data: result);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }

        [HttpHead]
        [WebApiAuthorization]
        public async Task<IActionResult> Head(string method, string param)
        {
            try
            {
                var codeidentify = await DoDynamicApiRequest(method, JESAI.Biz.Data.Enums.HttpType.Head);
                var result = await _uniformApiService.Execute(codeidentify, param);
                return Success(data: result);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }
    }
}
